{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们要介绍Numpy中的两个概念，一个是reshape，一个是transpose。其中，transpose很好理解，相当于矩阵的转置，而reshape顾名思义，相当于是变形的意思。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 2. , 3.3],\n",
       "       [4. , 5.2, 6.8]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.array([[1. , 2. ,3.3],[4. , 5.2, 6.8]])\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a2的形状是(2,3)，我们可不可以改变它的形状呢？比如将它变成3行2列的呢？答案是肯定的，只要我们调用reshape方法，将新的（3,2）的形状穿进去，就可以得到一个新的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 2. ],\n",
       "       [3.3, 4. ],\n",
       "       [5.2, 6.8]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.reshape(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.reshape(3,2).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "那既然数组可以改变形状，那我可以将a2变成100行，100列的么？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一定是不行的，一个数组可以变形的原则就是，变形后不能改变元素的个数。所以，对于a2变行后来说，(3,2)、(6，1）、（1，6）都是可以的，而(100,100)则是不可以的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们之所以要介绍reshape，是因为很多时候我们要对数组做乘积，可并非所有的数组都可以相乘，数组能相乘的规则就是，要么其中一个数组的维度是1，要么两个数组的形状是一样的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a2的形状是(2,3)，而a3的形状是(2,3,3)，二者的形状是不同的，所以执行相乘会报错。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3],\n",
       "        [ 4,  5,  6],\n",
       "        [ 7,  8,  9]],\n",
       "\n",
       "       [[ 7,  8,  9],\n",
       "        [10, 11, 12],\n",
       "        [13, 14, 15]]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = np.array([[[1, 2, 3], [4, 5, 6],[7, 8, 9]], [[7, 8, 9], [10, 11, 12], [13, 14, 15]]])\n",
    "a3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "为了让二者可以相乘，则需要将a2执行reshape操作，将其变为(2,3,1)，这样就与a3的形状一样，便可以与之相乘了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1. ],\n",
       "        [2. ],\n",
       "        [3.3]],\n",
       "\n",
       "       [[4. ],\n",
       "        [5.2],\n",
       "        [6.8]]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2_reshape = a2.reshape(2,3,1)\n",
    "a2_reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  1. ,   2. ,   3. ],\n",
       "        [  8. ,  10. ,  12. ],\n",
       "        [ 23.1,  26.4,  29.7]],\n",
       "\n",
       "       [[ 28. ,  32. ,  36. ],\n",
       "        [ 52. ,  57.2,  62.4],\n",
       "        [ 88.4,  95.2, 102. ]]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2_reshape * a3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "transpose就是矩阵的转置，比较容易理解，如果矩阵是2维，转置后就是行变列，列变行；如果是多维，转置后的形状就是原矩阵形状的逆序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 2. , 3.3],\n",
       "       [4. , 5.2, 6.8]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 4. ],\n",
       "       [2. , 5.2],\n",
       "       [3.3, 6.8]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3],\n",
       "        [ 4,  5,  6],\n",
       "        [ 7,  8,  9]],\n",
       "\n",
       "       [[ 7,  8,  9],\n",
       "        [10, 11, 12],\n",
       "        [13, 14, 15]]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3, 3)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3, 2)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3.T.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3],\n",
       "        [ 4,  5,  6],\n",
       "        [ 7,  8,  9]],\n",
       "\n",
       "       [[ 7,  8,  9],\n",
       "        [10, 11, 12],\n",
       "        [13, 14, 15]]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  7],\n",
       "        [ 4, 10],\n",
       "        [ 7, 13]],\n",
       "\n",
       "       [[ 2,  8],\n",
       "        [ 5, 11],\n",
       "        [ 8, 14]],\n",
       "\n",
       "       [[ 3,  9],\n",
       "        [ 6, 12],\n",
       "        [ 9, 15]]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2],\n",
       "        [ 3,  4],\n",
       "        [ 5,  6]],\n",
       "\n",
       "       [[ 7,  8],\n",
       "        [ 9,  7],\n",
       "        [ 8,  9]],\n",
       "\n",
       "       [[10, 11],\n",
       "        [12, 13],\n",
       "        [14, 15]]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3.reshape(3,3,2)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f0eadef1d77f15b2dc417c8cc1a73e43ff71cc62600b9af0dbbd14bf3385942a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
